package com.sitech.ibnms.util;

import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

public class GenerateName {

	/**
	 * @param unit_id
	 * @param kpi_id
	 * @return ˵���� �����ļ�������㷨
	 */
	public static String getCoreName(String unit_id) {
		unit_id = unit_id.replaceAll("\\.", "_"); // �����汧���»���
		// ������Ҫ�����ű�ı�ǰԵ,���unit_id�������ж�
		String table[];
		String prefix = "";
		String lastfix = "";
		String names = "";
		try {
			if (unit_id != null && unit_id.length() > 0
					&& unit_id.substring(0, 2).equals("10")) {

				table = unit_id.split(":");

				StringTokenizer prest = new StringTokenizer(table[0], "-");
				for (int i = 0; prest.hasMoreTokens() && i < 2; i++) {
					prefix = prefix + prest.nextToken();
				}
				StringTokenizer lastst = new StringTokenizer(table[1], "-");
				if (lastst.hasMoreTokens()) {
					lastfix = lastst.nextToken();
					lastfix = split(lastfix);
				}
				names = prefix + "_" + lastfix;
			}

			if (unit_id != null && unit_id.length() > 0
					&& !unit_id.substring(0, 2).equals("10")) {

				StringTokenizer prest = new StringTokenizer(unit_id, "-");
				for (int i = 0; prest.hasMoreTokens() && i < 3; i++) {
					prefix = prefix + prest.nextToken();
				}
				names = prefix;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return names;
	}

	public static String split(String str) {
		String string1[] = str.split("_");
		String result = "";
		try {
			for (int i = 0; i < string1.length; i++) {
				String str1 = string1[i];
				StringTokenizer prest = new StringTokenizer(str1, ".");
				int count = prest.countTokens();
				String string2[] = new String[count];

				for (int n = 0; n < count; n++) {
					string2[n] = prest.nextToken();
				}
				String ss = "";
				if (string2.length > 1) {
					ss = (String) string2[2] + (String) string2[3];
				} else {
					ss = str1;
				}

				result = result + ss + "_";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		result = result.substring(0, result.length() - 1);
		return result;
	}
	/**
	 * @param unit_id
	 * @param kpi_id
	 * @return ˵���� ��Щʵ��id�д���С��㣬oracle�н������쳣����������ת����ͳһת�������߻�
	 */
	public static String getFileName(String unit_id) {
		String tables = getCoreName(unit_id);
		String prex = "HIS_";
		Date cll_time = new Date();
		int month = cll_time.getMonth();
		int flag = month % 4;
		return prex + tables + "_" + flag;
	}
	
	public static String getTableName(String unit_id,int month) {
		String tables = getCoreName(unit_id);
		String prex = "HIS_";
		int flag = month % 4;
		return prex + tables + "_" + flag;
	}
	public static String getAllDataSql(String unit_id,Date sDate,Date eDate) {
		String str = "";
		Calendar cal = Calendar.getInstance();
		cal.setTime(eDate);//��ѯ����ʱ��
		int eMonth = cal.get(Calendar.MONTH);
		int eYear = cal.get(Calendar.YEAR);
		cal.setTime(sDate);//��ѯ��ʼʱ��
		int sMonth = cal.get(Calendar.MONTH);
		int sYear = cal.get(Calendar.YEAR);
		if(sYear==eYear){
			if(eMonth!=sMonth){
				int n = 0;
				for(int i = sMonth;i<=eMonth&&n<4;i++){
					String tbName1 = GenerateName.getTableName(unit_id, i);
					if(i==sMonth){
						str +=" select * from "+tbName1+" ";
					}else{
						str +=" union all select * from "+tbName1+" ";
					}
					n++;
				}
				str = "("+str+")";
			}else{
				str = GenerateName.getTableName(unit_id,sMonth);
			}
		}else{
			int n = 0;
			int end = (eYear-sYear)*12+eMonth;
			for(int i = sMonth;i<=end&&n<4;i++){
				String tbName1 = GenerateName.getTableName(unit_id, i);
				if(i==sMonth){
					str +=" select * from "+tbName1+" ";
				}else{
					str +=" union all select * from "+tbName1+" ";
				}
				n++;
			}
			str = "("+str+")";
		}
		
		return str;
	}
	/**
	 * ����ָ����Ҫ����ı���
	 * 
	 * @param unit_id
	 * @return
	 */
	public static String getTableName_PM(String unit_id) {
		return getFileName(unit_id);
	}

	public static void main(String[] args) {
		System.out.println(getTableName_PM("10-13-10-10-11:cisco_c3560-10108-interface"));
	}
}
